//
//  orderViewController.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/9/2.
//  Copyright (c) 2015年 szb. All rights reserved.
//

#import "orderViewController.h"
#import "orderModel.h"
#import "AccountTool.h"
#import "MBProgressHUD+MJ.h"
#import "addressChooseVC.h"
#import "AFNetworking.h"
#import "ZBCheckAlasset.h"
#import "AppDelegate.h"
#import "BPProductDetailPage.h"
#import "BPProductModel.h"
#import "MJExtension.h"
#import "BPhttpTool.h"
#import "payView.h"
#import "PayOverSuccessViewController.h"
#import "addressMangerVC.h"


#define ImageCahes [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]

@interface orderViewController ()<addressInfoDelegate>

@property (nonatomic,strong)BPAddressModel *address;

@property (nonatomic,strong)AppDelegate *delegate;

@property (nonatomic,strong)orderModel *orderData;

@property (nonatomic,copy)NSString *addressId;

@end

@implementation orderViewController


- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _index = 1;
    _meiIndex = 0;
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64)];
    [self.view addSubview:_scrollView];
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.alwaysBounceVertical = YES;
    
    [self downloadjifenNum];
    
    [self setupNav];
    
    [self setUpBaseData];
    
    [self setUpLabel];
    
    [self createButton];
}


// 创建 返回按钮
- (void)createButton{
    
    UIButton *buttonBig = [[UIButton alloc] initWithFrame:CGRectMake(20, 25, 60, 50)];
    buttonBig.backgroundColor = [UIColor clearColor];
    [buttonBig addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonBig];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 23, 23)];
    [button setBackgroundImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBig addSubview:button];
    
//    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 25, 23, 23)];
//    [button setBackgroundImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:button];
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 30, 20, 100, 30)];
    name.text = @"提交订单";
    name.font = [UIFont systemFontOfSize:20];
    name.textColor = BPFenHongColor;
    [self.view addSubview:name];
    
    UIImageView *im = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, 5)];
    im.image = [UIImage imageNamed:@"波纹线"];
    [self.view addSubview:im];
    
}
// 头部的 两个按钮
- (void)button:(UIButton *)b{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)downloadjifenNum
{
    [BPhttpTool get:[NSString stringWithFormat:@"%@user/info/uid/%@",BPhttp,[AccountTool account].uid] params:nil success:^(id responseObj) {
        //NSLog(@"%@",responseObj[@"res"]);
        NSDictionary *dic = responseObj[@"res"];
        NSString *score = dic[@"score"];
        BPAccount *myAccount = [AccountTool account];
        myAccount.score = score;
        //NSLog(@"%@",score);
        [AccountTool save:myAccount];
        _meiYinNum.text = score;
    } failure:^(NSError *error) {
        
    }];
}

- (void)setupNav{
    self.title = @"提交订单";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithButtonName:@"返回" tatget:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"地址选择" style:UIBarButtonItemStyleDone target:self action:@selector(chooseAnotherAddress)];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setUpBaseData
{
    // 原来是 最终价格 , 由于 从"我的作品" 进入价格会搞错, 那么 现在把价格改为 price, 目前 从哪里进入 价格 都是对的.
     productPrice = [self.productModel.finalPrice floatValue];
    shipFee = [self.productModel.shipfee floatValue];
    lastPrice = shipFee + productPrice;
//    节约
    save = lastPrice - payPrice;
}

- (void)setUpLabel
{
    // 结算之后的详情的页面
    NSArray *placeArry = [NSArray arrayWithObjects:@"数量",@"运费",@"小计",@"美印币数量",@"使用数量",@"节约",@"应付",@"收货地址", nil];
    CGFloat topDistance = 10.0f;
    CGFloat labelH = 20.0f;
    CGFloat bottomDistance = 10.0f;
    CGFloat labelX = BPStatusCellInset;
    CGFloat labelW = BPScreenW - labelX * 2.0;
    CGFloat allH = topDistance + labelH + bottomDistance;
    
    // 制作模板的 名字
    UILabel *firstlabel = [[UILabel alloc] initWithFrame:CGRectMake(labelX, 0, labelW / 2.0, allH)];
    firstlabel.text = self.productModel.title;
    firstlabel.font = [UIFont systemFontOfSize:16.0f];
    [_scrollView addSubview:firstlabel];
    
    
    for (int i = 0; i < 8; i ++) {
        BPLineView *line = [[BPLineView alloc] initWithFrame:CGRectMake(labelX, allH * (i+1), labelW, 0.7)];
        [_scrollView addSubview:line];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, (allH) * (i + 1), labelW / 2.0, allH)];
        [_scrollView addSubview:label];
        label.text = placeArry[i];
        label.textColor = [UIColor grayColor];
        label.font = [UIFont systemFontOfSize:14.0f];
    }
    BPLineView *line = [[BPLineView alloc] initWithFrame:CGRectMake(labelX, allH * 6, labelW, 0.7)];
    [_scrollView addSubview:line];
    
    [self setUpChooseAddressView];
    
    
    
    //设置提交按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(0, allH *8.8 + 130.0f + 40.0f, self.view.frame.size.width, 50.0f);
    [btn.layer setMasksToBounds:YES];
    [btn.layer setCornerRadius:0];
    btn.backgroundColor = BPGlobaThemeColor;
    [btn setTitle:@"马上支付" forState:UIControlStateNormal];
    [btn setTintColor:[UIColor whiteColor]];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _scrollView.contentSize = CGSizeMake(BPScreenW, btn.y + btn.height);
    
    // 数量
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat btnW = 20.0f;
    CGFloat btnY = (allH)*2 + allH / 2.0 - (btnW / 2.0);
    addButton.frame = CGRectMake(labelW - 10.0, btnY - 40, btnW, btnW);
    [addButton setImage:[UIImage imageNamed:@"addBtn"] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(addNumber) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:addButton];
    CGFloat indexW = 30.0f;
    _productIndex = [[UILabel alloc] initWithFrame:CGRectMake(addButton.x - 5.0f - indexW, addButton.y, indexW, btnW)];
    _productIndex.text = [NSString stringWithFormat:@"%ld",(long)_index];
    _productIndex.textAlignment = NSTextAlignmentCenter;
    _productIndex.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    _productIndex.layer.borderWidth = 1.0f;
    [_scrollView addSubview:_productIndex];
    UIButton *reduceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    reduceButton.frame = CGRectMake(_productIndex.x - 5.0f - btnW, _productIndex.y, btnW, btnW);
    [reduceButton setImage:[UIImage imageNamed:@"reduceBtn"] forState:UIControlStateNormal];
    [reduceButton addTarget:self action:@selector(reduceNumber) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:reduceButton];
    
    // 美印币的个数(可以加减)
    UIButton *addButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    addButton2.frame = addButton.bounds;
    addButton2.y = btnY + 3*allH;
    addButton2.x = addButton.x;
    [addButton2 setImage:[UIImage imageNamed:@"addBtn"] forState:UIControlStateNormal];
    [addButton2 addTarget:self action:@selector(addNumber2) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:addButton2];
    
    _meiyin = [[UILabel alloc] initWithFrame:_productIndex.bounds];
    _meiyin.y = _productIndex.y + allH*4;
    _meiyin.x = _productIndex.x;
    _meiyin.text = [NSString stringWithFormat:@"%ld",(long)_meiIndex];
    _meiyin.textAlignment = NSTextAlignmentCenter;
    _meiyin.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    _meiyin.layer.borderWidth = 1.0f;
    [_scrollView addSubview:_meiyin];
    
    UIButton *reduceButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    reduceButton2.frame = reduceButton.bounds;
    reduceButton2.y = reduceButton.y + 4*allH;
    reduceButton2.x = reduceButton.x;
    [reduceButton2 setImage:[UIImage imageNamed:@"reduceBtn"] forState:UIControlStateNormal];
    [reduceButton2 addTarget:self action:@selector(reduceNumber2) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:reduceButton2];

    
    UIButton *chooseAdr = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat btnWs = 80.0f;
    CGFloat btnHs = 23.0f;
    // 其他地址
    chooseAdr.frame = CGRectMake(BPScreenW / 1.4, allH * 8.2, btnWs, btnHs);
    [chooseAdr setBackgroundColor:BPGlobaThemeColor];
    [chooseAdr setTitle:@"其他地址" forState:UIControlStateNormal];
    chooseAdr.titleLabel.font = [UIFont boldSystemFontOfSize:13.0f];
    [chooseAdr.layer setMasksToBounds:YES];
    [chooseAdr.layer setCornerRadius:3.0f];
    [chooseAdr setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [chooseAdr addTarget:self action:@selector(chooseAnotherAddress) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:chooseAdr];
    
    
    //设置右边label ( 单价 )
    _singelPrice = [[UILabel alloc] initWithFrame:CGRectMake(BPScreenW / 2.0, 0, labelW / 2.0, allH)];
    [self addRightLabel:_singelPrice Addprice:productPrice];

    // 运费
    _expressPrice = [[UILabel alloc] initWithFrame:CGRectMake(BPScreenW / 2.0, allH * 2, labelW / 2.0, allH)];
    [self addRightLabel:_expressPrice Addprice:shipFee];

    
    // 小计
    _xiaoJi = [[UILabel alloc] initWithFrame:CGRectMake(BPScreenW / 2.0, allH * 3, labelW / 2.0, allH)];
    [self addRightLabelXiaoJi:_xiaoJi Addprice:lastPrice];
    [self countFinalPrice];
    
    // 美印币数量
    _meiYinNum = [[UILabel alloc] initWithFrame:CGRectMake(BPScreenW / 2.0, allH * 4, labelW / 2.0, allH)];
    [self addRightLabelMeiYin:_meiYinNum Addprice:0];
    
    
    
    // 节约
    _jieyue = [[UILabel alloc] initWithFrame:CGRectMake(BPScreenW / 2.0, allH * 6, labelW / 2.0, allH)];
    [self addRightLabel:_jieyue Addprice:0];
    
    
    // 实际应该付的总价
    _allPrice = [[UILabel alloc] initWithFrame:CGRectMake(BPScreenW / 2.0, allH * 7, labelW / 2.0, allH)];
    [self addRightLabelXiaoJi:_allPrice Addprice:lastPrice];
    // 判断是否包邮
    [self countFinalPrice];
    
   
}

- (void)countFinalPrice
{
    //应付的总价
    CGFloat price = productPrice * _index;
    
    // 根据 数量变化的  "小计"
    if ([_productIndex.text isEqualToString:@"0"]) {
        CGFloat Zprice = 0;
        NSString *str = [NSString stringWithFormat:@"%ld",(long)Zprice];
        _xiaoJi.font = [UIFont systemFontOfSize:17];
        _xiaoJi.text = [NSString stringWithFormat:@"%@%@", @"¥", str];
    }else{
        
        CGFloat Zprice = price + shipFee;
        NSString *str = [NSString stringWithFormat:@"%ld",(long)Zprice];
        _xiaoJi.font = [UIFont systemFontOfSize:17];
        _xiaoJi.text = [NSString stringWithFormat:@"%@%@", @"¥", str];
    }
    
    
    //1.判断是否包邮
    if (price < 88.0) {
        price = price + shipFee;
    }
    
    //判断美印币
    price = price - _meiIndex;
    if (price < 0.01) {
        price = 0.01;
    }
    
    if (_index == 0) {
        price = 0;
    }
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%.2f",price]];
    NSRange contentRange = {1,[content length] - 1};
    [content addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18.0f] range:contentRange];
    _allPrice.attributedText = content;
    payPrice = price;
}



//增加数量
- (void)addNumber
{
    _index = _index + 1;
    _productIndex.text = [NSString stringWithFormat:@"%ld",(long)_index];
    
    [self countFinalPrice];
}

- (void)reduceNumber
{
    if (_index > 0) {
        _index = _index - 1;
        _productIndex.text = [NSString stringWithFormat:@"%ld",(long)_index];
         [self countFinalPrice];
    }
}
//增加美印币
- (void)addNumber2
{
    if (_meiIndex < [[AccountTool account].score intValue] ) {
        if (_meiIndex < (int)((productPrice * _index) / 2)) {
            _meiIndex = _meiIndex + 1;
            _meiyin.text = [NSString stringWithFormat:@"%ld",(long)_meiIndex];
            _jieyue.text = _meiyin.text;
            [self countFinalPrice];
        }else{
            [MBProgressHUD showError:@"不能超过总价的一半"];
        }
        
    }else{
        [MBProgressHUD showError:@"美印币不足"];
    }
}

- (void)reduceNumber2
{
    if (_meiIndex > 0) {
        _meiIndex = _meiIndex - 1;
        _meiyin.text = [NSString stringWithFormat:@"%ld",(long)_meiIndex];
        
        _jieyue.text = _meiyin.text;
         [self countFinalPrice];
    }else{
        [MBProgressHUD showError:@"不能再少"];
    }
}

- (void)addRightLabel:(UILabel *)rightLabel Addprice:(CGFloat)price
{
    rightLabel.font = [UIFont systemFontOfSize:12.0f];
    rightLabel.textAlignment = NSTextAlignmentRight;
    rightLabel.textColor = [UIColor grayColor];
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%.2f",price]];
    NSRange contentRange = {1,[content length] - 1};
    [content addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18.0f] range:contentRange];
    rightLabel.attributedText = content;
    [_scrollView addSubview:rightLabel];
}


- (void)addRightLabelMeiYin:(UILabel *)rightLabel Addprice:(CGFloat)price
{
    rightLabel.font = [UIFont systemFontOfSize:12.0f];
    rightLabel.textAlignment = NSTextAlignmentRight;
    rightLabel.textColor = [UIColor grayColor];
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%.2f",price]];
    NSRange contentRange = {0,[content length]};
    [content addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18.0f] range:contentRange];
    rightLabel.attributedText = content;
    [_scrollView addSubview:rightLabel];
}


- (void)addRightLabelXiaoJi:(UILabel *)rightLabel Addprice:(CGFloat)price
{
    rightLabel.font = [UIFont systemFontOfSize:13.0f];
    rightLabel.textAlignment = NSTextAlignmentRight;
    rightLabel.textColor = BPGlobaThemeColor;
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%.2f",price]];
    NSRange contentRange = {1,[content length] - 1};
    [content addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18.0f] range:contentRange];
    rightLabel.attributedText = content;
    [_scrollView addSubview:rightLabel];
}


// 收货地址
- (void)setUpChooseAddressView
{
    
    defultManger = [[CoreDateManager alloc] init];
    
    self.address = [defultManger getDefaultAddressWithuid:[AccountTool account].uid];
    _addrInfo = [[addressInfoView alloc] initWithFrame:CGRectMake(0, 360.0f, BPScreenW, 130.0f)];
    _addrInfo.delegate = self;
    [_scrollView addSubview:_addrInfo];
    if (self.address.province == nil) {
        _noAddress = [[UILabel alloc] initWithFrame:CGRectMake(0, 360.0f, BPScreenW, 130.0f)];
        _noAddress.backgroundColor = BPGlobaBGColor;
        _noAddress.text = @"请选择您的收货地址";
        _noAddress.textAlignment = NSTextAlignmentCenter;
        _noAddress.textColor = [UIColor whiteColor];
        _noAddress.font = [UIFont systemFontOfSize:20.0f];
        UITapGestureRecognizer *choose = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseAnotherAddress)];
        [_noAddress addGestureRecognizer:choose];
        [_scrollView addSubview:_noAddress];
        
    }else{
        [_addrInfo addData:self.address];
    }
}

- (void)chooseAnotherAddress
{
    addressChooseVC *chooseAddress = [[addressChooseVC alloc] init];
    chooseAddress.block=^(BPAddressModel *model){
        //替换默认收获地址
        if (self.address.addressid != model.addressid) {
            //前面那个参数为新默认地址的id
            [defultManger updateDefaultAddress:model.addressid lastAddress:self.address.addressid withuid:[AccountTool account].uid];
        }
        self.address = model;
        //更新ui
        [_addrInfo addData:model];
        _noAddress.hidden = YES;
        _noAddress = nil;
    };
    [self.navigationController pushViewController:chooseAddress animated:YES];
    
}

- (void)btnClick
{
    orderModel *orderData = [[orderModel alloc] init];
    orderData.uid = [AccountTool account].uid;
    orderData.goodid = self.productModel.iD;
    orderData.works_id = self.productModel.workID;
//    orderData.price = [NSString stringWithFormat:@"%.2f",payPrice + _meiIndex];
    orderData.price = [NSString stringWithFormat:@"%.2f",payPrice];
    
    orderData.num = _index;
    orderData.score = _meiIndex;
    orderData.addressid = self.address.addressid;
    orderData.info = self.address.time;
    self.orderData = orderData;
    
//    BPAccount *account2 = [AccountTool account];
//    NSString *jifen = account2.score; 
//    int score = [jifen intValue];
//    score = score - _meiIndex;
//    jifen = [NSString stringWithFormat:@"%d",score];
//    account2.score = jifen;
//    [AccountTool save:account2];
    
    if ([_addrInfo checkAddress] == NO) {
        [MBProgressHUD showError:@"请填写正确收货信息"];
    }else{
        [self orderSuccess];
    }
}

- (void)orderSuccess
{
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    // 当网络状态改变了，就会调用
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:// 未知网络
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                [MBProgressHUD showError:@"请在wifi状态下上传"];
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                if ([self.productModel.over isEqual:@"1"]) {
                    [self orderSuccess2];
                }
                else{
                    [self uploadAlasset];
                }
                
                break;
        }
    }];
    [mgr startMonitoring];
}

- (void)uploadAlasset
{
    // 监听 -> 图片是否正在上传 (如果正在上传,那么不能再进行上传的操作)
    _delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    if (_delegate.productIsPost == NO) {
        _delegate.productIsPost = YES;
        ZBCheckAlasset *postAlasset = [[ZBCheckAlasset alloc] init];
        
        [postAlasset checkAlaaserArry:self.assetArry withView:self.view];
        postAlasset.dismissBlock = ^(){
            //[self backTohomeVC];
            [MBProgressHUD showMessage2:@"正在上传请稍后"];
        };
        postAlasset.ZBblock = ^(){
            [self postProductData];

        };
    }else
    {
        [MBProgressHUD showError:@"正在上传请等待"];
    }
}

- (void)backTohomeVC
{
//    if (_delegate.isEdit == YES) {
    NSLog(@"返回到了主页");
    
        [self.navigationController popToRootViewControllerAnimated:YES];
//    }else{
//        _delegate.backHome = YES;
//        [self dismissViewControllerAnimated:YES completion:nil];
//    }
}

- (void)postProductData
{
    NSArray *pageArry = [NSArray array];
    pageArry = self.productModel.pages;
    int i = 1;
    for (BPProductDetailPage *page in pageArry) {
        page.page = i;
        i++;
        if (page.moulds != nil) {
            for (BPProductModel *model in page.moulds) {
                //NSLog(@"%@",model.alassetStr);
                //alassetStr就是照片的url的字符串
                // 给照片取出唯一标识
                NSString *key = model.alassetStr;
                NSRange range = [model.alassetStr rangeOfString:@"id="];
                key = [key substringFromIndex:range.location];
                key = [key stringByReplacingOccurrencesOfString:@"-" withString:@""];
                key = [key substringWithRange:NSMakeRange(3, 28)];
                model.photo = key;
            }
            //将model对象转化为数组类型
            page.moulds = [BPProductModel keyValuesArrayWithObjectArray:page.moulds];
        }
    }
    pageArry = [BPProductDetailPage keyValuesArrayWithObjectArray:pageArry];
    //NSLog(@"%@",pageArry);
    NSData *jsondata = [NSJSONSerialization dataWithJSONObject:pageArry options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsondata encoding:NSUTF8StringEncoding];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:self.productModel.workID forKey:@"works_id"];
    [params setValue:jsonString forKey:@"pages"];
    [params setValue:[NSString stringWithFormat:@"%f",self.productModel.bigScale] forKey:@"scale"];
    //NSLog(@"%@",params);
    //配置好的信息传给服务器
    [BPhttpTool post:[NSString stringWithFormat:@"%@works/savepage",BPhttp] params:params success:^(id responseObj) {
        //NSLog(@"%@",responseObj);
        self.productModel.over = @"1";
        [self orderSuccess2];
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"上传作品失败"];
        _delegate.productIsPost = NO;
    }];
}

- (void)orderSuccess2
{
    NSDictionary *param = [NSDictionary dictionary];
    param = [self.orderData keyValues];
    [BPhttpTool post:[NSString stringWithFormat:@"%@order/commit",BPhttp] params:param success:^(id responseObj) {
        NSDictionary *dic = responseObj;
        if ([dic[@"status"] intValue] == 1) {
            _addressId = dic[@"info"];
//            successAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"作品上传成功，是否立即前往支付" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
//            [successAlert show];
            [self createNext];
            [defultManger removeOneProduct:self.productModel.makeDate withUID:[AccountTool account].uid];
        }else{
            
            [_delegate showDefeatView];
            [_delegate defeatView].orderData = self.orderData;
        }
        _delegate.productIsPost = NO;
        //self.submitBtn.enabled = YES;
    } failure:^(NSError *error) {
        
        [_delegate showDefeatView];
        [_delegate defeatView].orderData = self.orderData;
        _delegate.productIsPost = NO;
    }];
}

//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    if (buttonIndex == 1) {
//        [_delegate showPayView];
//        [[_delegate payView] setUpHeadView:_addressId];
//        [_delegate payView].price = self.orderData.price;
//        [_delegate payView].orderNumber = _addressId;
//        [_delegate payView].block = ^(){};
//    }
//    
//}

- (void)createNext{
    [_delegate showPayView];
    //[[_delegate payView] setUpHeadView:_addressId];
    [_delegate payView].price = self.orderData.price;
    [_delegate payView].orderNumber = _addressId;

    
    [_delegate payView].block = ^(){
    
        PayOverSuccessViewController *pa = [[PayOverSuccessViewController alloc] init];
        pa.dingDan = [_delegate payView].Danhao;
        [self.navigationController pushViewController:pa animated:YES];
    };
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
